home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-20
/
rs0422.zip
/
LEVEL3
/
SORTSYM.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-11-04
|
4KB
|
252 lines
#include <string.h>
#undef NULL
#include <conio.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
FILE *fnami, *fnamo;
char zarr[100],fname[80];
#define STRMAX 10
#define STRLEN 7
struct inrec {
unsigned int val;
char name[STRMAX];
};
#define SORTMAX 1000
struct inrec sort_tab[SORTMAX];
int key_tab[SORTMAX];
int tabmax;
int
getlin(str,max)
char *str;
int max;
{
int ch, pos;
ch=pos=0;
while (ch != 0x0a && pos<max) {
ch=getchar();
if (ch == -1) return -1;
str[pos++]=toupper(ch);
str[pos]=0;
}
if (ch== 0x0a) str[pos-1]=0;
return (strlen(str));
}
int
fgetlin(str,max,f)
char *str;
int max;
FILE *f;
{
int ch, pos;
ch=pos=0;
while (ch != 0x0a && pos<max) {
ch=getc(f);
if (ch == -1) return -1;
str[pos++]=toupper(ch);
str[pos]=0;
}
if (ch== 0x0a) str[pos-1]=0;
return (strlen(str));
}
int
fgetline(str,max,f)
char *str;
int max;
FILE *f;
{
int l;
while ((l=fgetlin(str, max, f)) == 0) ;
return l;
}
int
getline(str,max)
char *str;
int max;
{
int l;
while ((l=getlin(str, max)) == 0) ;
return l;
}
int
fputline(s,f)
char *s;
FILE *f;
{
while (*s) fputc(*s++,f);
fputc(0x0a,f);
}
int
get_yes()
{
register char ch;
do {
ch=getch();
ch=toupper(ch);
} while ( (ch!=0x59) && (ch!=0x4e));
return (ch == 0x59); /* True is Yes */
}
putxch(val, f)
FILE f;
int val;
{
char v;
v= (val >> 4) &0x0f;
if (v > 9) v += 7;
fputc(v+0x30, f);
v=(val & 0x0f);
if (v > 9) v += 7;
fputc(v+0x30, f);
}
unsigned int
gethex(ch)
char **ch;
{
unsigned int v;
int i,j;
register char *c;
c = *ch;
v = 0;
while (*c == 0x20) c++;
while (*c) {
i = toupper(*c++);
v = v<<4;
if (i >=0x30 && i <= 0x39) v += (i&0x0f);
else if (i >= 0x41 && i <= 0x46) v += (9 + (i&0x0f));
else break;
if (*c == 0x20) break;
}
while (*c == 0x20) c++;
*ch = c;
return v;
}
int
main(argc,argv)
int argc;
char *argv[];
{
int i, j, l, dif;
unsigned int v;
char *ch, *in, *out;
struct inrec wrk;
in = out = NULL;
if (argc == 3) {
in = argv[1];
out = argv[2];
}
do {
if (in) /* Name supplied in command line */ {
fnami=fopen(in,"rt");
if (fnami == NULL) {
printf("%s does not exist.\n",in);
in = NULL;
}
} else {
printf("Symbol file to read? ");
getline(fname, 80);
fnami=fopen(fname,"rt");
}
} while (fnami == NULL);
do {
if (out) /* Name supplied in command line */ {
fnamo=fopen(out,"wt");
if (fnamo == NULL) {
printf("Can't write to %s\n",out);
out = NULL;
}
} else {
printf("Symbol file to create? ");
getline(fname, 80);
fnamo=fopen(fname,"wt");
}
} while (fnamo == NULL);
tabmax = 0;
for (i=0;i<SORTMAX;i++) {
sort_tab[i].val = 0;
sort_tab[i].name[0]=NULL;
key_tab[i] = -1;
}
while (!feof(fnami)) {
l=fgetline(zarr,80,fnami);
if (l>0) {
ch = zarr;
key_tab[tabmax] = tabmax;
sort_tab[tabmax].val=gethex(&ch);
i=0;
if (*ch == 0x5f) ch++;
while (*ch && i<STRLEN) sort_tab[tabmax].name[i++] = *ch++;
while (i<STRMAX) sort_tab[tabmax].name[i++] = 0x20;
sort_tab[tabmax].name[STRMAX-1]=0;
j = 0; /* Note side effects of J */
for (i=0;i<tabmax;i++) /* Sort on to the list */ {
dif = strcmp(sort_tab[key_tab[i]].name,
sort_tab[key_tab[tabmax]].name);
if (dif>0) {
v = key_tab[tabmax];
key_tab[tabmax] = key_tab[i];
key_tab[i] = v;
j = TRUE;
}
else if (!j && !dif) {
if (sort_tab[key_tab[i]].val ==
sort_tab[key_tab[tabmax]].val) {
tabmax--; /* Dupe */
break;
}
printf("%s is not unique! (%s)\n", sort_tab[tabmax].name, zarr);
}
}
if (++tabmax >= SORTMAX) {
printf(" Symbol Table EOF at %d ! \n",tabmax);
break;
}
}
}
fclose(fnami);
printf("\nThere were %d Symbols read.\n\n",tabmax);
zarr[0]=0;
for (j=0;j<tabmax;j++) {
i = key_tab[j];
if (70 < sprintf(zarr, "%s%4.4X %s ",
zarr, sort_tab[i].val, sort_tab[i].name)) {
fputline(zarr,fnamo);
zarr[0]=0;
}
}
if (strlen(zarr) > 0) fputline(zarr,fnamo);
/* All Done, close files */
fclose(fnamo);
return 0;
}